package com.lkd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lkd.dto.NodeDto;
import com.lkd.entity.Node;
import com.lkd.mapper.NodeMapper;
import com.lkd.result.PageBean;
import com.lkd.service.*;
import com.lkd.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class NodeServiceImpl implements NodeService {

    @Autowired
    private NodeMapper nodeMapper;
    @Autowired
    private RegionService regionService;
    @Autowired
    private BusinessService businessService;
    @Autowired
    private VendingMachineService vendingMachineService;
    @Autowired
    private VmTypeService vmTypeService;

    @Override
    public PageBean<NodeVo> findByNodePage(Integer pageIndex, Integer pageSize, String name,String regionId) {
        PageHelper.startPage(pageIndex, pageSize);
        List<NodeVo> voList = nodeMapper.selectByNodeCondition(name,regionId);
        RegionVo regionVo = regionService.selectRegionById(name,regionId);
        BusinessVo businessVo = businessService.selectBusinessById(name,regionId);
        for (NodeVo vo : voList) {
            vo.setRegion(regionVo);
            vo.setBusinessType(businessVo);
        }
        Page<NodeVo> page = (Page<NodeVo>) voList;
        return new PageBean(pageIndex, pageSize, (long) page.getPages(), page.getTotal(), page.getResult());
    }

    @Override
    public List<BusinessVo> findBusinessType() {
        List<BusinessVo> voList =  nodeMapper.selectBusinessType();
        return voList;
    }

    @Override
    public void save(NodeVo nodeVo) {
        nodeMapper.insert(nodeVo);
    }

    @Override
    public List<VendingMachineVo> findById(Long nodeId) {
        List<VendingMachineVo> voList = vendingMachineService.selectByNodeId(nodeId);
        VmTypeVo vmTypeVo = vmTypeService.selectByNodeId(nodeId);

        RegionVo regionVo1 = regionService.selectRegionByNodeId(nodeId);
        BusinessVo businessVo = businessService.selectBusinessByNodeId(nodeId);
        NodeVo nodeVo = nodeMapper.selectByNodeId(nodeId);
        nodeVo.setRegion(regionVo1);
        nodeVo.setBusinessType(businessVo);

        RegionVo regionVo = regionService.selectByNodeId(nodeId);

        for (VendingMachineVo vo : voList) {
            vo.setType(vmTypeVo);
            vo.setNode(nodeVo);
            vo.setRegion(regionVo);
        }
        return voList;
    }

    @Override
    public void update(NodeDto dto) {
//        对象属性拷贝
        Node node = BeanUtil.toBean(dto, Node.class);
        nodeMapper.updateById(node);
    }

    @Override
    public void delete(Long nodeId) {
        nodeMapper.deleteById(nodeId);
    }

    @Override
    public NodeVo selectByInnerCode(String innerCode) {
        return nodeMapper.selectByInnerCode(innerCode);
    }

    @Override
    public int count() {
        int count = nodeMapper.count();
        return count;
    }
}
